package com.alog.yzd.bookmall.web.filter;

import com.alog.yzd.bookmall.domain.user.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        User user = (User) req.getSession().getAttribute("sessionUser");
        if(user != null){
            chain.doFilter(request, response);
        } else {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "您还没有登陆,不能访问本资源");
            req.getRequestDispatcher("/jsps/msg.jsp").forward(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
